package com.example.litterpainterdemo.draw.shapes

import android.graphics.Canvas
import kotlinx.coroutines.flow.flow
import kotlin.math.abs

class CircleShape:Shape() {
    private var left = 0f
    private var top = 0f
    private var right = 0f
    private var bottom = 0f
    override fun insideTheShape(dx: Float, dy: Float): Boolean {
        val cx = (left + right)/2
        val cy = (top + bottom)/2
        val vr = abs(top-bottom) /2
        val hr = abs(left-right) /2
        val a = Math.max(vr,hr)
        val b = Math.min(vr,hr)
        val result =  ((dx - cx) * (dx - cx) / (a * a)) + ((dy - cy) * (dy - cy) / (b * b))
        return result <= 1.0f
    }

    override fun drawShape(canvas: Canvas?) {
        left = Math.min(startPoint.x,endPoint.x)
        right = Math.max(startPoint.x,endPoint.x)
        top = Math.min(startPoint.y,endPoint.y)
        bottom = Math.max(startPoint.y,endPoint.y)
        canvas?.drawOval(left,top,right,bottom,mPaint)
    }
}